// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.asm.analysis;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.analysis.AnalyzerException;
import org.objectweb.asm.tree.analysis.BasicInterpreter;
import org.objectweb.asm.tree.analysis.Value;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class NewInstanceInterpreter extends InterpreterAdapter {
  public static final class NewInstanceValue implements Value {
    private final TypeInsnNode newInsn;

    NewInstanceValue(TypeInsnNode newInsn) {
      this.newInsn = newInsn;
    }
    
    public int getSize() {
      return 1;
    }
    
    public TypeInsnNode getNewInsn() {
      return newInsn;
    }
  }
  
  public NewInstanceInterpreter() {
    super(new BasicInterpreter());
  }

  @Override
  public Value newOperation(AbstractInsnNode insn) throws AnalyzerException {
    if (insn.getOpcode() == Opcodes.NEW) {
      return new NewInstanceValue((TypeInsnNode) insn);
    } else {
      return super.newOperation(insn);
    }
  }
}
